
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    reason:'',
    recordDate:'',
    pay_type:'',
    time_popup_show:false,
    typeOption:[],
    type_popup_show:false
  },

  /**
   * 选择支付方式
   */
  choosePayType(){
    this.setData({
      type_popup_show:true
    })
  },

  /**支付方式确认 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      pay_type:value,
      type_popup_show:false
    })
   
  },
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },
  
/**选择时间 */
  chooseTime(e){
    let currentTimestamp = this.data.recordDate?new Date(api.rowTocol(this.data.recordDate)).getTime():new Date().getTime();
    this.setData({
      time_popup_show:true,
      currentTimestamp:currentTimestamp
    })
  },
  /**日期选择确认 */
  timeConfirm(event){

    let date = new Date(event.detail);
      this.setData({
        recordDate: api.formatDateCRM('Y-m-d',date),
        time_popup_show:false,
      })
   
  },
  timeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  commit(e){
    let para = e.detail.value;
    let fileList = this.data.fileList;
    let fileId = [];
    fileList.forEach((v,i)=>{
      fileId.push(v.id)
    })
    para.fileId = JSON.stringify(fileId);
    para.id=this.data.info.id?this.data.info.id:'',
    api.curlPost('?cla=paymentApp&fun=commitEdit',para).then((res)=>{
      wx.redirectTo({
        url: '../paymentInfo/paymentInfo?id='+res.data.id
      })
    })
  },



  /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;

    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'payment',fileType:'付款申请'}).then((res)=>{
      // 上传完成需要更新 fileList
           const { fileList = [] } = that.data;
           fileList.push({ ...file, url: res.result.src,id:res.result.id });
           that.setData({ fileList });
         })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
  
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'payment'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = api.isBlank(options.id)?'':options.id;
    let that = this;

    api.curlPost('?cla=paymentApp&fun=editPage',{id:id}).then((res)=>{
        that.setData({
          info:res.data.result,
          recordDate:res.data.result.recordDate,
          pay_type:res.data.result.payType,
          typeOption:res.data.result.payTypeOption,
          fileList:res.data.result.attachFile?res.data.result.attachFile:[]
        })
      })
    
  },
 
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})